﻿
//***********************************************************************
// * Module:   MsjXjJdcbdqzmHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/29
// * Purpose: Definition of the Class MsjXjJdcbdqzmHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjXjJdcbdqzmHandler 的摘要说明
    /// </summary>
    public class MsjXjJdcbdqzmHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                MsjXjJdcbdqzm mMsjXjJdcbdqzm = new MsjXjJdcbdqzm();
                mMsjXjJdcbdqzm.Sqid = Guid.NewGuid().ToString(); //主键
                mMsjXjJdcbdqzm.Xm = context.Request.Form["texXm"]; //姓名
                mMsjXjJdcbdqzm.Sfzh = context.Request.Form["texSfzh"]; //身份证号
                mMsjXjJdcbdqzm.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                mMsjXjJdcbdqzm.Zz = context.Request.Form["texZz"]; //住址
                mMsjXjJdcbdqzm.Cphm = context.Request.Form["texCphm"]; //车牌号码
                mMsjXjJdcbdqzm.Clppxh = context.Request.Form["texClppxh"]; //车辆品牌型号
                mMsjXjJdcbdqzm.Clsbhm = context.Request.Form["texClsbhm"]; //车辆识别号码
                mMsjXjJdcbdqzm.Fdjhm = context.Request.Form["texFdjhm"]; //发动机号码
                mMsjXjJdcbdqzm.Cljyqk = context.Request.Form["texCljyqk"]; //车辆简要情况
                mMsjXjJdcbdqzm.Bdh = context.Request.Form["texBdh"]; //保单号
                mMsjXjJdcbdqzm.Bxgsmc = context.Request.Form["texBxgsmc"]; //保险公司名称
                mMsjXjJdcbdqzm.Basj = context.Request.Form["texBasj"]; //报案时间
                mMsjXjJdcbdqzm.Baqk = context.Request.Form["texBaqk"]; //报案情况
                mMsjXjJdcbdqzm.Bafs = context.Request.Form["texBafs"]; //报案方式
                mMsjXjJdcbdqzm.Ajsldw = context.Request.Form["texAjsldw"]; //案件受理单位
                mMsjXjJdcbdqzm.Qxsgaj = context.Request.Form["texQxsgaj"]; //区县市公安局
                mMsjXjJdcbdqzm.Qxsgaj2 = context.Request.Form["texQxsgaj2"]; //派出所或刑警中队
                mMsjXjJdcbdqzm.Slr = context.Request.Form["texSlr"]; //受理人
                mMsjXjJdcbdqzm.Sqrqz = context.Request.Form["texSqrqz"]; //申请人签字
                mMsjXjJdcbdqzm.Qzrq = context.Request.Form["texQzrq"]; //签字日期
                mMsjXjJdcbdqzm.Sljg = context.Request.Form["texSljg"]; //受理机关
                mMsjXjJdcbdqzm.Cjsj = DateTime.Now.ToString(); //创建时间(2013-05-28 09:13:01)
                mMsjXjJdcbdqzm.Cjr = mUser.UserId; //创建人(人员ID)
                mMsjXjJdcbdqzm.Cjrdw = mUser.OrgnaId; //创建单位(单位ID)
                //mMsjXjJdcbdqzm.Xgr = context.Request.Form["texXgr"]; //修改人(人员ID)
                //mMsjXjJdcbdqzm.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                //mMsjXjJdcbdqzm.Xgsj = context.Request.Form["texXgsj"]; //修改时间(2013-05-28 09:13:01)
                mMsjXjJdcbdqzm.Enabled = "1"; //是否有效（1有效，0无效）
                mMsjXjJdcbdqzm.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjXjJdcbdqzmBLL.Insert(mMsjXjJdcbdqzm, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjXjJdcbdqzm mMsjXjJdcbdqzm = new MsjXjJdcbdqzm();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjXjJdcbdqzmBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjXjJdcbdqzm mMsjXjJdcbdqzm = new MsjXjJdcbdqzm();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjXjJdcbdqzm = MsjXjJdcbdqzmBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjXjJdcbdqzm != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mMsjXjJdcbdqzm.Xm = context.Request.Form["texXm"]; //姓名
                    mMsjXjJdcbdqzm.Sfzh = context.Request.Form["texSfzh"]; //身份证号
                    mMsjXjJdcbdqzm.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                    mMsjXjJdcbdqzm.Zz = context.Request.Form["texZz"]; //住址
                    mMsjXjJdcbdqzm.Cphm = context.Request.Form["texCphm"]; //车牌号码
                    mMsjXjJdcbdqzm.Clppxh = context.Request.Form["texClppxh"]; //车辆品牌型号
                    mMsjXjJdcbdqzm.Clsbhm = context.Request.Form["texClsbhm"]; //车辆识别号码
                    mMsjXjJdcbdqzm.Fdjhm = context.Request.Form["texFdjhm"]; //发动机号码
                    mMsjXjJdcbdqzm.Cljyqk = context.Request.Form["texCljyqk"]; //车辆简要情况
                    mMsjXjJdcbdqzm.Bdh = context.Request.Form["texBdh"]; //保单号
                    mMsjXjJdcbdqzm.Bxgsmc = context.Request.Form["texBxgsmc"]; //保险公司名称
                    mMsjXjJdcbdqzm.Basj = context.Request.Form["texBasj"]; //报案时间
                    mMsjXjJdcbdqzm.Baqk = context.Request.Form["texBaqk"]; //报案情况
                    mMsjXjJdcbdqzm.Bafs = context.Request.Form["texBafs"]; //报案方式
                    mMsjXjJdcbdqzm.Ajsldw = context.Request.Form["texAjsldw"]; //案件受理单位
                    mMsjXjJdcbdqzm.Qxsgaj = context.Request.Form["texQxsgaj"]; //区县市公安局
                    mMsjXjJdcbdqzm.Qxsgaj2 = context.Request.Form["texQxsgaj2"]; //派出所或刑警中队
                    mMsjXjJdcbdqzm.Slr = context.Request.Form["texSlr"]; //受理人
                    mMsjXjJdcbdqzm.Sqrqz = context.Request.Form["texSqrqz"]; //申请人签字
                    mMsjXjJdcbdqzm.Qzrq = context.Request.Form["texQzrq"]; //签字日期
                    mMsjXjJdcbdqzm.Sljg = context.Request.Form["texSljg"]; //受理机关
                    //mMsjXjJdcbdqzm.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                    //mMsjXjJdcbdqzm.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjXjJdcbdqzm.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                    mMsjXjJdcbdqzm.Xgr = mUser.UserId; //修改人(人员ID)
                    mMsjXjJdcbdqzm.Xgrdw = mUser.OrgnaId; //修改人单位(单位ID)
                    mMsjXjJdcbdqzm.Xgsj = DateTime.Now.ToString(); //修改时间(2013-05-28 09:13:01)
                    mMsjXjJdcbdqzm.Enabled = "1"; //是否有效（1有效，0无效）
                    //mMsjXjJdcbdqzm.Movesign = context.Request.Form["texMovesign"]; //是否传输（1已，0未）

                    mResultModel = MsjXjJdcbdqzmBLL.Update(mMsjXjJdcbdqzm, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjXjJdcbdqzm mMsjXjJdcbdqzm = new MsjXjJdcbdqzm(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjXjJdcbdqzm = MsjXjJdcbdqzmBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjXjJdcbdqzm != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjXjJdcbdqzm);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjXjJdcbdqzm = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            string name = context.Request.Form["texName"];
            string tel = context.Request.Form["texTel"];
            string carno = context.Request.Form["txcCarno"];
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjXjJdcbdqzmConst.Columns.Enabled, "1"), SqlLogicOper.AND_LOGIC);
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjXjJdcbdqzmConst.Columns.Xm, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(tel))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjXjJdcbdqzmConst.Columns.Lxdh, tel), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(carno))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjXjJdcbdqzmConst.Columns.Cphm, '%' + carno + '%'), SqlLogicOper.AND_LOGIC);
            }

            dtMsjXjJdcbdqzm = Q.GetRecordByPage("*", sqllist.ToSql(), "BASJ", intPageSize, intPageIndex, MsjXjJdcbdqzmConst.TableName, MsjXjJdcbdqzmConst.FieldKey, "Desc").Tables[0];
            if (dtMsjXjJdcbdqzm != null && dtMsjXjJdcbdqzm.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjXjJdcbdqzm.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjXjJdcbdqzm.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjXjJdcbdqzm;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


